import requests
import time
import base64

# 应用账号（请替换为真实账号）
app_key = 'app_key'
# 应用密码（请替换为真实密码）
app_secret = 'app_secret'

authKey = app_key + ':' + app_secret;
authKeyBase64 =  str(base64.b64encode(authKey.encode('utf-8')), "utf-8")

headers = {
    # "Accept": "application/json, text/javascript, */*; q=0.01",
    # "Accept-Encoding": "gzip, deflate, br",
    # "Accept-Language": "zh-CN,zh;q=0.9",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
    'Proxy-Authorization': 'Basic ' + authKeyBase64
}
api_url = "https://api.xiaoxiangdaili.com/ip/get"

def getProxy():
    res = requests.get(api_url, params={'appKey': app_key, 'appSecret': app_secret, 'wt': 'text', 'cnt': 1})
    content = str(res.content,'utf-8')
    print("API response: " + content)
    return content

def getUrlContent(url):
    p = getProxy()
    # 如需使用socks5代理，只需将下面http替换为socks5h即可
    proxyMeta = "http://%(proxy)s" % {
        "proxy": p
    }
    proxies = {
        'http': proxyMeta,
        'https': proxyMeta,
    }
    try:
        resp = requests.get(url=url, proxies=proxies, headers=headers)
        resp.encoding = 'utf-8'
    except Exception as e:
        print(e)
    else:
        print(resp.text)


target_url = "http://cip.cc"
for i in range(3):
    getUrlContent(target_url)
    time.sleep(10)
